package com.htyy.yutao.myapplication1;

import com.htyy.yutao.configure.*;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;


public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    private WebView wView;
    private TextView btn_back;
    private TextView btn_top;
    private TextView btn_refresh;
    private long exitTime = 0;

    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled", "AddJavascriptInterface"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();//隐藏掉整个ActionBar
        setContentView(R.layout.activity_main2);


        wView = (WebView) findViewById(R.id.wView);
        btn_back = (TextView) findViewById(R.id.btn_back);
        btn_top = (TextView) findViewById(R.id.btn_top);
        btn_refresh = (TextView) findViewById(R.id.btn_refresh);

        btn_back.setOnClickListener(this);
        btn_refresh.setOnClickListener(this);
        btn_top.setOnClickListener(this);

//        wView.loadUrl("file:///android_asset/www/demo1.html");
        wView.loadUrl("http://www.baidu.com");
//        WebSettings webSettings = wView.getSettings();
        //设置WebView允许调用js
//        webSettings.setJavaScriptEnabled(true);
//        webSettings.setDefaultTextEncodingName("UTF-8");
        wView.setWebViewClient(new WebViewClient() {
            //在webview里打开新链接
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        //将object对象暴露给Js
        wView.addJavascriptInterface(new MyObject(Main2Activity.this), "myObj");




    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                finish();          //关闭当前Activity
                break;
            case R.id.btn_refresh:
                wView.reload();    //刷新当前页面
                break;
            case R.id.btn_top:
                wView.setScrollY(0);   //滚动到顶部
                break;
        }
    }



}
